package com.lizardmind.haofudoctor.other.rongYun

import android.content.Context
import android.text.Spannable
import android.text.SpannableString
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.linxiao.framework.util.color
import com.linxiao.framework.util.ke
import com.lizardmind.haofudoctor.R
import com.lizardmind.haofudoctor.other.OnClickRongDiagnoseEvent
import com.lizardmind.haofudoctor.other.OnClickRongFanganEvent
import com.lizardmind.haofudoctor.other.OnClickRongVisitEvent
import io.rong.imkit.model.ProviderTag
import io.rong.imkit.model.UIMessage
import io.rong.imkit.widget.provider.IContainerItemProvider
import io.rong.imlib.model.Message
import org.greenrobot.eventbus.EventBus
import org.jetbrains.anko.backgroundColor
import org.jetbrains.anko.find
import org.jetbrains.anko.toast

/**
 *
 * Created by Extends on 2017/12/4 16:12
 */
@ProviderTag(messageContent = HaofuMessage::class)
class HaofuMessageItemProvider :IContainerItemProvider.MessageProvider<HaofuMessage>() {
    override fun onItemClick(p0: View, p1: Int, p2: HaofuMessage, p3: UIMessage) {
//        ke(p3.targetId)
        when(p2.type){
            "0"->EventBus.getDefault().post(OnClickRongDiagnoseEvent(p2.id,p3.messageDirection == Message.MessageDirection.SEND))
            "1"->EventBus.getDefault().post(OnClickRongFanganEvent(p2.id,p3.messageDirection == Message.MessageDirection.SEND))
            "2"->EventBus.getDefault().post(OnClickRongVisitEvent(p2.id,p3.messageDirection == Message.MessageDirection.SEND))
            else->{p0.context.toast("消息类型错误")}
        }
    }

    override fun bindView(v: View, position: Int, content: HaofuMessage, message: UIMessage) {
        val holder = v.tag as ViewHolder
        if(message.messageDirection == Message.MessageDirection.SEND){//消息方向，自己发送的
            when(content.type){
                "0"->{
                    v.setBackgroundResource(R.drawable.ry_ic_diagnose_right)
                    holder.title?.setText(R.string.ry_diagnose_title_d)
                    holder.title?.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.ry_diagnose_orvisit,0,0,0)
                    holder.title?.backgroundColor = v.context.color(R.color.ry_color_diagnose)
                    holder.message?.setText(R.string.ry_diagnose_message_d)
                }
                "1"->{
                    v.setBackgroundResource(R.drawable.ry_ic_fangan_right)
                    holder.title?.setText(R.string.ry_fangan_title_d)
                    holder.title?.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.ry_fangan,0,0,0)
                    holder.title?.backgroundColor = v.context.color(R.color.ry_color_fangan)
                    holder.message?.setText(R.string.ry_fangan_message_d)
                }
                "2"->{
                    v.setBackgroundResource(R.drawable.ry_ic_visit_right)
                    holder.title?.setText(R.string.ry_visit_title_d)
                    holder.title?.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.ry_visit,0,0,0)
                    holder.title?.backgroundColor = v.context.color(R.color.ry_color_visit)
                    holder.message?.setText(R.string.ry_visit_message_d)
                }
            }
        }else {
            when(content.type){
                "0"->{
                    v.setBackgroundResource(R.drawable.ry_ic_diagnose_left)
                    holder.title?.setText(R.string.ry_diagnose_title_u)
                    holder.title?.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.ry_diagnose_orvisit,0,0,0)
                    holder.title?.backgroundColor = v.context.color(R.color.ry_color_diagnose)
                    holder.message?.setText(R.string.ry_diagnose_message_u)
                }
                "1"->{
                    //用户不应该返回方案给医生，所以没有此项
                }
                "2"->{
                    v.setBackgroundResource(R.drawable.ry_ic_visit_left)
                    holder.title?.setText(R.string.ry_visit_title_u)
                    holder.title?.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.ry_diagnose_orvisit,0,0,0)
                    holder.title?.backgroundColor = v.context.color(R.color.ry_color_visit)
                    holder.message?.setText(R.string.ry_visit_message_u)
                }
            }
        }
    }

    override fun getContentSummary(p0: HaofuMessage): Spannable {
       return when(p0.type){
            "0"->SpannableString("[问诊单消息]")
            "1"->SpannableString("[方案消息]")
            "2"->SpannableString("[复诊单消息]")
           else -> SpannableString("[问诊单消息]")
        }
    }

    override fun newView(p0: Context, p1: ViewGroup): View {
        val view = LayoutInflater.from(p0).inflate(R.layout.ry_item_haofu,p1,false)
        val holder = ViewHolder()
        holder.title = view.find(R.id.tv0)
        holder.message = view.find(R.id.tv1)
        view.tag = holder
        return view
    }

    class ViewHolder{
        var title:TextView?=null
        var message:TextView?=null
    }
}